package com.baikeyang.app.controller.admin.activity;


import com.alibaba.fastjson.JSON;
import com.baikeyang.common.bean.PageResult;
import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.constant.QiniuConstants;
import com.baikeyang.common.model.ActivityFileLibrary;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.service.activity.IActivityFileLibraryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.google.gson.Gson;
//import com.qiniu.common.QiniuException;
//import com.qiniu.common.Zone;
//import com.qiniu.http.Response;
//import com.qiniu.storage.Configuration;
//import com.qiniu.storage.UploadManager;
//import com.qiniu.storage.model.DefaultPutRet;
//import com.qiniu.util.Auth;
//import com.qiniu.util.StringMap;
//import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.*;

/**
 * <p>
 * 活动媒体文件库 前端控制器
 * </p>
 *
 * @author BaiKeyang
 * @since 2019-05-15
 */
@RestController(value = "mediaLibraryController")
@RequestMapping("/admin/activity/mediaLibrary")
public class ActivityFileLibraryController {

    @Autowired
    private IActivityFileLibraryService activityFileLibraryService;

    @RequestMapping(value = "index")
    private ModelAndView index () {
        return new ModelAndView("/admin/activity/mediaLib/activity-media-index");
    }

    @RequestMapping(value = "list")
    public Object getActivityTypes (String page, String recPerPage, String search, String sortBy, String order) {
        PageResult pagers = new PageResult();
        try{
            Integer size = 10;
            Integer current = 1;
            if(StringUtils.isNotEmpty(recPerPage)){
                try {
                    size = Integer.parseInt(recPerPage);
                } catch (Exception e) {}
            }
            if(StringUtils.isNotEmpty(page)) {
                try {
                    current = Integer.parseInt(page);
                    if(current < 1){current = 1;}
                } catch (Exception e) {}
            }
            Page<ActivityFileLibrary> pageParam = new Page<>(current, size);
            IPage<ActivityFileLibrary> pageResult = activityFileLibraryService.loadActivityFilePage(pageParam);
            pagers.setData(pageResult.getRecords());
            pagers.setPager(current, pageResult.getTotal());
            pagers.getPager().setRecPerPage(Integer.parseInt(recPerPage));
            pagers.setResult("success");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return pagers;
    }

    @RequestMapping(value = "upload", method = RequestMethod.GET)
    public ModelAndView uploadIndex () {
        return new ModelAndView("/admin/activity/mediaLib/activity-media-upload");
    }

    @RequestMapping(value = "uploadw", method = RequestMethod.GET)
    public ModelAndView uploadWIndex () {
        return new ModelAndView("/admin/activity/mediaLib/activity-media-uploadw");
    }

    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public ResponseResult upload (@RequestParam(value = "file") MultipartFile multipartFile,
                                  @RequestParam(value = "uid", required = false) String uid,
                                  @RequestParam(value = "album", required = false) String album,
                                  @RequestParam(value = "id") String id,
                                  @RequestParam(value = "name") String name,
                                  @RequestParam(value = "size") Integer size,
                                  @RequestParam(value = "type") String type,
                                  @RequestParam(value = "md5", required = false) String md5,
                                  @RequestParam(value = "lastModifiedDate") Date lastModifiedDate,
                                  Integer chunks, Integer chunk,
        HttpServletRequest request, HttpServletResponse response) {
            ResponseResult responseResult = null;
            try {
                responseResult = activityFileLibraryService.saveFile(multipartFile, id, name, size, type, lastModifiedDate,chunks , chunk, md5, album, request);
            } catch (IOException e) {
                return new ResponseResult(e.getMessage(), ExecuteConstants.EXECUTECODE.CODE_500);
            }
        return responseResult;
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    public ResponseResult delete (@PathVariable(value = "id") String id,
                                  HttpServletRequest request, HttpServletResponse response) {
        return activityFileLibraryService.deleteFile(id);
    }

    /**
     * 根据文件MD5查询文件是否存在
     * @param md5 文件唯一指纹
     * @return
     */
    @RequestMapping(value="/check",method=RequestMethod.POST)
    public ResponseResult checkFile (@RequestParam(value = "md5") String md5){
        return activityFileLibraryService.checkFile(md5);
    }

    /**
     *
     * 检测分片是否已经存在
     * @param md5 文件唯一指纹
     * @param chunk 分片编号
     * @param chunkSize 分片大小
     * @return
     */
    @RequestMapping (value = "checkChunk", method = RequestMethod.POST)
    public ResponseResult checkChunk(String md5, Integer chunk, Integer chunkSize, HttpServletRequest request, HttpServletResponse response) {
        return activityFileLibraryService.checkChunk(md5, chunk, chunkSize, request);
    }


    public static void main(String[] args) {
        System.out.println(com.baikeyang.common.util.UUID.getUUID());
    }


}

